package com.cms.web.exportQueryAnalyse;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.pub.util.EMPExportExcelTable;
import com.web.BaseController;

import com.cms.biz.queryAnalyse.CMSQueryAnalyseServiceMgr;
import com.efounder.builder.base.data.EFDataSet;
import com.efounder.builder.base.data.EFRowSet;
import com.efounder.eai.data.JParamObject;
import com.efounder.eai.data.JResponseObject;
import com.enums.EMPQueryParamEnum;

@Controller
@RequestMapping(value="/queryAnalyse/export")
public class EMPExportCMSCCTZQueryAnalyseController extends BaseController{
	@Autowired
	private                CMSQueryAnalyseServiceMgr                       queryAnalyseServiceMgr;
	
	@Autowired
	public HttpSession session;

	/**
	 * 生产单剩余数量
	 * @param F_CKBH
	 * @param model
	 * @return
	 */
	@RequestMapping("/CCTZAnalyseQuery")
	public ModelAndView CCTZQueryAnalyse(@RequestParam String F_QSRQ, @RequestParam String F_JZRQ, @RequestParam String F_HTBH, 
            							 @RequestParam String F_XMBH, @RequestParam String F_TXM, 
            							 HttpServletRequest request, HttpServletResponse response, Model model) {
		JParamObject           po = JParamObject.Create();
		EFDataSet        columnDS = EFDataSet.getInstance();
		EFRowSet         columnRS = null;
		EFDataSet    queryDataSet = null;
		EFRowSet      queryRowSet = null;
		List<String>  subHeadList = new ArrayList<String>();
		String         BILL_WHERE = " CCTZXX.F_HTBH like '" + F_HTBH + "%' and CCTZXX.F_XMBH like '" + F_XMBH + "%' and "
        						  + " CCTZXX.F_ZDRQ >= '" + F_QSRQ + "' and CCTZXX.F_ZDRQ <= '" + F_JZRQ + "'";

		if(!F_TXM.equals("")) {
		BILL_WHERE += " and CCTZXX.F_KJQJ = '" + F_TXM.substring(0, 6) + "' and CCTZXX.F_DJBH = '" + F_TXM.substring(6) + "'";
		}	
		
		po.SetValueByParamName("CTN_FCT1", "CCTZXX");
		po.SetValueByParamName("BILL_WHERE", BILL_WHERE);
		po.SetValueByParamName("BILL_ORDER", "CCTZXX.F_KJQJ desc, CCTZXX.F_DJBH desc");
		
		JResponseObject        RO = queryAnalyseServiceMgr.searchSCSQ(po);
		queryRowSet = (EFRowSet) RO.getResponseObject();

		queryDataSet = queryRowSet.getDataSet(EMPQueryParamEnum.QUERYRESULT);
		try {
			columnRS = EFRowSet.getInstance();
			columnRS.putString("COLUMN_ID", "F_KJQJ");
			columnRS.putString("COLUMN_ALIGN", "LEFT");
			columnRS.putString("COLUMN_NAME", "会计期间");
			columnRS.putString("COLUMN_WIDTH", "100");
			columnRS.putString("COLUMN_TYPE", "C");
			columnDS.insertRowSet(columnRS);
			
			columnRS = EFRowSet.getInstance();
			columnRS.putString("COLUMN_ID", "F_DJBH");
			columnRS.putString("COLUMN_ALIGN", "LEFT");
			columnRS.putString("COLUMN_NAME", "单据编号");
			columnRS.putString("COLUMN_WIDTH", "150");
			columnRS.putString("COLUMN_TYPE", "C");
			columnDS.insertRowSet(columnRS);
			
			columnRS = EFRowSet.getInstance();
			columnRS.putString("COLUMN_ID", "F_HTBH");
			columnRS.putString("COLUMN_ALIGN", "LEFT");
			columnRS.putString("COLUMN_NAME", "合同编号");
			columnRS.putString("COLUMN_WIDTH", "150");
			columnRS.putString("COLUMN_TYPE", "C");
			columnDS.insertRowSet(columnRS);
			
			columnRS = EFRowSet.getInstance();
			columnRS.putString("COLUMN_ID", "F_HTBHMC");
			columnRS.putString("COLUMN_ALIGN", "LEFT");
			columnRS.putString("COLUMN_NAME", "合同名称");
			columnRS.putString("COLUMN_WIDTH", "300");
			columnRS.putString("COLUMN_TYPE", "C");
			columnDS.insertRowSet(columnRS);
			
			columnRS = EFRowSet.getInstance();
			columnRS.putString("COLUMN_ID", "F_XMBH");
			columnRS.putString("COLUMN_ALIGN", "LEFT");
			columnRS.putString("COLUMN_NAME", "项目编号");
			columnRS.putString("COLUMN_WIDTH", "150");
			columnRS.putString("COLUMN_TYPE", "C");
			columnDS.insertRowSet(columnRS);
			
			columnRS = EFRowSet.getInstance();
			columnRS.putString("COLUMN_ID", "F_XMBHMC");
			columnRS.putString("COLUMN_ALIGN", "LEFT");
			columnRS.putString("COLUMN_NAME", "项目名称");
			columnRS.putString("COLUMN_WIDTH", "300");
			columnRS.putString("COLUMN_TYPE", "C");
			columnDS.insertRowSet(columnRS);
			
			columnRS = EFRowSet.getInstance();
			columnRS.putString("COLUMN_ID", "F_JSQD");
			columnRS.putString("COLUMN_ALIGN", "LEFT");
			columnRS.putString("COLUMN_NAME", "结算渠道");
			columnRS.putString("COLUMN_WIDTH", "150");
			columnRS.putString("COLUMN_TYPE", "C");
			columnDS.insertRowSet(columnRS);
			
			columnRS = EFRowSet.getInstance();
			Map<String, String> maskMap = new HashMap<String, String>();
			columnRS.putString("COLUMN_ID", "F_SFCQ");
			columnRS.putString("COLUMN_ALIGN", "LEFT");
			columnRS.putString("COLUMN_NAME", "是否澄清技术方案");
			columnRS.putString("COLUMN_WIDTH", "120");
			columnRS.putString("COLUMN_TYPE", "C");
			maskMap.put("0", "否");
			maskMap.put("1", "是");
			columnRS.setExtProperty("MASKMAP", maskMap);
			columnDS.insertRowSet(columnRS);
			
			columnRS = EFRowSet.getInstance();
			columnRS.putString("COLUMN_ID", "F_JSFZRMC");
			columnRS.putString("COLUMN_ALIGN", "LEFT");
			columnRS.putString("COLUMN_NAME", "技术负责人名称");
			columnRS.putString("COLUMN_WIDTH", "120");
			columnRS.putString("COLUMN_TYPE", "C");
			columnDS.insertRowSet(columnRS);
			
			
			columnRS = EFRowSet.getInstance();
			columnRS.putString("COLUMN_ID", "F_NOTE");
			columnRS.putString("COLUMN_ALIGN", "LEFT");
			columnRS.putString("COLUMN_NAME", "备注");
			columnRS.putString("COLUMN_WIDTH", "500");
			columnRS.putString("COLUMN_TYPE", "C");
			columnDS.insertRowSet(columnRS);
			
			columnRS = EFRowSet.getInstance();
			columnRS.putString("COLUMN_ID", "F_ZDRMC");
			columnRS.putString("COLUMN_ALIGN", "LEFT");
			columnRS.putString("COLUMN_NAME", "制单人名称");
			columnRS.putString("COLUMN_WIDTH", "100");
			columnRS.putString("COLUMN_TYPE", "C");
			columnDS.insertRowSet(columnRS);
			
			columnRS = EFRowSet.getInstance();
			columnRS.putString("COLUMN_ID", "F_ZDRQ");
			columnRS.putString("COLUMN_ALIGN", "LEFT");
			columnRS.putString("COLUMN_NAME", "制单日期");
			columnRS.putString("COLUMN_WIDTH", "100");
			columnRS.putString("COLUMN_TYPE", "D");
			columnDS.insertRowSet(columnRS);
			//下载
			EMPExportExcelTable exportExcelTable = new EMPExportExcelTable();
			exportExcelTable.exportExcelTable(request, response, queryDataSet, columnDS, subHeadList, "出 厂 通 知 信 息");			
		} catch (Exception e) {

			e.printStackTrace();
		}
		return ajaxDoneError("导出成功！");
	}
	
	/**
	 * 出厂内容查询
	 * @param F_CKBH
	 * @param model
	 * @return
	 */
	@RequestMapping("/CCNRAnalyseQuery")
	public ModelAndView CCNRAnalyseQuery(@RequestParam String F_XMBH, @RequestParam String F_CPBH, 
                                   HttpServletRequest request, HttpServletResponse response, Model model) {
		JParamObject           po = JParamObject.Create();
		EFDataSet    queryDataSet = null;
		EFRowSet      queryRowSet = null;
		EFDataSet        columnDS = EFDataSet.getInstance();
		EFRowSet         columnRS = null;
		List<String>  subHeadList = new ArrayList<String>();
		
		po.SetValueByParamName("F_XMBH", F_XMBH);
		po.SetValueByParamName("F_CPBH", F_CPBH);
		
		JResponseObject        RO = queryAnalyseServiceMgr.searchCCNR(po);
		queryRowSet = (EFRowSet) RO.getResponseObject();
		
		queryDataSet = queryRowSet.getDataSet(EMPQueryParamEnum.QUERYRESULT);
		try {
			columnRS = EFRowSet.getInstance();
			columnRS.putString("COLUMN_ID", "F_XMBH");
			columnRS.putString("COLUMN_ALIGN", "LEFT");
			columnRS.putString("COLUMN_NAME", "项目编号");
			columnRS.putString("COLUMN_WIDTH", "150");
			columnRS.putString("COLUMN_TYPE", "C");
			columnDS.insertRowSet(columnRS);
			
			columnRS = EFRowSet.getInstance();
			columnRS.putString("COLUMN_ID", "F_XMMC");
			columnRS.putString("COLUMN_ALIGN", "LEFT");
			columnRS.putString("COLUMN_NAME", "项目名称");
			columnRS.putString("COLUMN_WIDTH", "300");
			columnRS.putString("COLUMN_TYPE", "C");
			columnDS.insertRowSet(columnRS);
			
			columnRS = EFRowSet.getInstance();
			columnRS.putString("COLUMN_ID", "F_CPBH");
			columnRS.putString("COLUMN_ALIGN", "LEFT");
			columnRS.putString("COLUMN_NAME", "产品编号");
			columnRS.putString("COLUMN_WIDTH", "150");
			columnRS.putString("COLUMN_TYPE", "C");
			columnDS.insertRowSet(columnRS);
			
			columnRS = EFRowSet.getInstance();
			columnRS.putString("COLUMN_ID", "F_CPMC");
			columnRS.putString("COLUMN_ALIGN", "LEFT");
			columnRS.putString("COLUMN_NAME", "产品名称");
			columnRS.putString("COLUMN_WIDTH", "300");
			columnRS.putString("COLUMN_TYPE", "C");
			columnDS.insertRowSet(columnRS);
			
			columnRS = EFRowSet.getInstance();
			columnRS.putString("COLUMN_ID", "F_SCSL");
			columnRS.putString("COLUMN_ALIGN", "RIGHT");
			columnRS.putString("COLUMN_NAME", "生产数量");
			columnRS.putString("COLUMN_WIDTH", "100");
			columnRS.putString("COLUMN_TYPE", "N");
			columnDS.insertRowSet(columnRS);
			
			columnRS = EFRowSet.getInstance();
			columnRS.putString("COLUMN_ID", "F_CCSL");
			columnRS.putString("COLUMN_ALIGN", "RIGHT");
			columnRS.putString("COLUMN_NAME", "出厂数量");
			columnRS.putString("COLUMN_WIDTH", "100");
			columnRS.putString("COLUMN_TYPE", "N");
			columnDS.insertRowSet(columnRS);
			
			columnRS = EFRowSet.getInstance();
			columnRS.putString("COLUMN_ID", "F_SYSL");
			columnRS.putString("COLUMN_ALIGN", "RIGHT");
			columnRS.putString("COLUMN_NAME", "剩余数量");
			columnRS.putString("COLUMN_WIDTH", "100");
			columnRS.putString("COLUMN_TYPE", "N");
			columnDS.insertRowSet(columnRS);
			//下载
			EMPExportExcelTable exportExcelTable = new EMPExportExcelTable();
			exportExcelTable.exportExcelTable(request, response, queryDataSet, columnDS, subHeadList, "出 厂 内 容 信 息");			
		} catch (Exception e) {

			e.printStackTrace();
		}
		return ajaxDoneError("导出成功！");
	}
}
